package io.dbw.common.filter;

import io.dbw.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * @author lxc
 * @description:
 * @date 2021/9/29 15:02
 */
@Slf4j
public class AuthorityInterceptor implements HandlerInterceptor {

    private long startTimeMillis;

    private long endTimeMillis;
    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object object) throws Exception {
        startTimeMillis = System.currentTimeMillis();
        log.info("applet is request,URL:[{}] start .... ", new Object[] { request.getRequestURL()});
        return true;
    }

    /**
     * 请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv) throws Exception {

    }

    /**
     * 在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行
     * （主要是用于进行资源清理工作）
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) throws Exception {
        endTimeMillis = System.currentTimeMillis();
        log.info("applet is request,URL:[{}] end .... ,耗时: {}", new Object[] { request.getRequestURL(), DateUtils.formatDateTime(endTimeMillis - startTimeMillis)});
    }
}